/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo1029;

import ch06.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class BanjiDAO {
    public Banji[] findAll() {
        Connection conn = null;
        PreparedStatement pstat = null;
        ResultSet rs = null;
        Banji bjs[]=null;
        int count;
        try {
            conn = DBConnection.getConnection();
            pstat = conn.prepareStatement("select * from Banji");
            rs = pstat.executeQuery();
            rs.last();
            count=rs.getRow();
            bjs=new Banji[count];
            rs.beforeFirst();
            int i=0;
            while(rs.next()) {
                bjs[i]=new Banji(rs.getString(1),rs.getString(2));
                i++;
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return bjs;
    }
    
    
    public ArrayList<Banji> findAll2() {
        Connection conn = null;
        PreparedStatement pstat = null;
        ResultSet rs = null;
        ArrayList<Banji> bjs=new ArrayList<Banji>(); 
        int count;
        try {
            conn = DBConnection.getConnection();
            pstat = conn.prepareStatement("select * from Banji");
            rs = pstat.executeQuery();
            while(rs.next()) {
                Banji bj=new  Banji(rs.getString(1),rs.getString(2));
                bjs.add(bj);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return bjs;
    }
    
     public boolean findByUserNameAndPassword1(String username, String password) {
        Connection conn = null;
        PreparedStatement pstat = null;
        ResultSet rs = null;
        boolean flag=false;
        try {
            conn = DBConnection.getConnection();
            pstat = conn.prepareStatement("select * from user"
                    + " where username=?  and password=?");
            pstat.setString(1, username);
            pstat.setString(2, password);
            rs = pstat.executeQuery();
            if (rs.next()) {
                flag= true;
            } 
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            

        }
        return flag;

    }
}
